<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\Request;
use app\admin\service\Serialnumber as SerialnumberService;
use app\admin\validate\Serialnumber as SerialnumberValidate;
use think\facade\Db;

class Serialnumber  extends Base
{
    protected $request;
    protected $service;
    protected $validate;
    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-09 13:06:22 
     * @param [type] @required
     * @return Renderable
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new SerialnumberService();
        $this->validate = new SerialnumberValidate();
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-09 13:05:53 
     * @param [type] @required
     * @return Renderable
     */
    public function resource(Request $request)
    {
        $params = $this->request->params;
        $field = $params['field'];
        if (empty($field)) $field = 'all';
        $res = $this->service->getFieldEnum($field);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }

    /**
     * 显示编辑资源列表.
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function index()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }


        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $product_name = isset($params['product_name'])   ? $params['product_name'] : null;
        $sn_id = isset($params['sn_id'])   ? $params['sn_id']   : null;
        $status = (isset($params['status']) && $params['status'] !== '') ? intval($params['status']) : null;
        $start_time = isset($params['start_time'])   ? $params['start_time'] : null;
        $end_time = isset($params['end_time'])   ? $params['end_time'] : null;

        $where = [];
        if ($product_name) {
            $pids = Db::name('product')->where(function ($query) use ($product_name) {
                $query->where('cname', 'like', '%' . $product_name . '%')->whereOr('abbr', 'like', '%' . $product_name . '%');
            })->where('is_del', 0)->column('id');
            $where[] = ['pid', 'in', $pids];
        }

        if ($sn_id) {
            $where[] = ['sn_id', 'like', '%' . $sn_id . '%'];
        }

        if ($status !== null) {
            $where[] = ['status', '=', $status];
        }

        if ($start_time) {
            $where[] = ['create_time', '>=', $start_time];
        }

        if ($end_time) {
            $where[] = ['create_time', '<', $end_time];
        }

        $field = 'sn_id,seller_id,pid,wid,create_time,creator,status';

        $res =  $this->service->getList($pageIndex, $pageSize, $where, $field);

        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }


    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('save')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }
        $params['creator'] = $this->request->operator_id;
        $res = $this->service->save($params);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $res = $this->service->getDetail($id);

        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }

    public function discard()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('discard')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }
        $res = $this->service->discard($params);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-09 16:04:32 
     * @param [type] @required
     * @return Renderable
     */
    public function scanCode()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('scanCode')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }
        $params['creator'] = $this->request->operator_id;
        $res = $this->service->scanCode($params);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }


    public function scanlog()
    {
        $params = $this->request->params;
        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $product_name = isset($params['product_name'])   ? $params['product_name'] : null;
        $sn_id = isset($params['sn_id'])   ? $params['sn_id'] : null;
        $start_time = isset($params['start_time'])   ? $params['start_time'] : null;
        $end_time = isset($params['end_time'])   ? $params['end_time'] : null;

        $where = [];
        if ($product_name) {
            $pids = Db::name('product')->where(function ($query) use ($product_name) {
                $query->where('cname', 'like', '%' . $product_name . '%')->whereOr('abbr', 'like', '%' . $product_name . '%');
            })->where('is_del', 0)->column('id');
            $where[] = ['pid', 'in', $pids];
        }

        if ($sn_id) {
            $where[] = ['sn_id', 'like', '%' . $sn_id . '%'];
        }

        if ($start_time) {
            $where[] = ['create_time', '>=', $start_time];
        }

        if ($end_time) {
            $where[] = ['create_time', '<', $end_time];
        }

        $field = 'id,wid,action,sn_id,pid,create_time,creator';
        $res = $this->service->getScanlog($where, $pageIndex, $pageSize, $field);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-10 15:31:29 
     * @param [type] @required
     * @return Renderable
     */
    public function generateSnPictures()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('generateSnPictures')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }
        $res = $this->service->generateSnPictures($params);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }
}
